<div class="breadcrumb">
    <ul id="breadcrumb">
        <li><a href="<?php echo Configure::read('base_url');?>backstage" title="Home">
		<?php echo $this->Html->image("home.png", array("alt" => "Home",'class'=>'home','border'=>'0'));?>
		</a></li>
        <li><a href="<?php echo Configure::read('base_url');?>backstage/messages" title="Messages">Messages</a></li>
        <li>Archive</li>

    </ul>
</div>
<?php echo $this->element('admin/admin_leftmenu'); ?>
<div id="left">
    <?php
    echo $this->Session->flash();
    if ($this->Session->check('Message.auth'))
        echo $this->Session->flash('auth');
    ?>
    <div class="div_header"><?php __('Archive'); ?></div>
    <div class="div_content">
        <div class="paging">
            <?php echo $this->element('admin/message/top_menu'); ?>
	</div>
         <div >
            <form method="post" id="searchfrm" action="<?php echo Configure::read('base_url')."backstage/messages/msg_search"; ?>">
<table width="70%" border="0" cellspacing="0" cellpadding="0">
<tr>
    <td  style="border:0px;text-align: right;">Search Messages:</td><td   style="border:0px; width: 50px; text-align: left;"><input type="text" size="28"  name="serchmsg" class="required" title="Please enter keyword.">
</td><td  style="border:0px;"><select name="data[type]">
    <option value="">select</option>
    <option value="inbox">Inbox</option>
    <option value="sent">Sent Items</option>
    <option value="archive">Archive</option>
</select><input type="submit" style="background:#73AE0B;
color:#fff; font-size:14px;" name="button" id="button" value="Search" />
</td>
</tr>
</table>          
</form>
  <div id='message_success'></div>
         </div>
        <div>
            <input type="button" class="delete_selected" value="Delete Selected" name="Delete Selected" />&nbsp;
            <input type="button" class="star_selected" value="Star Selected" name="Star Selected" />&nbsp;
            <input type="button" class="unstar_selected" value="Unstar Selected" name="Unstar Selected" />&nbsp;
            <input type="button" class="move_selected" value="Move Selected to Inbox/Sent Items" name="INbox Selected" />&nbsp;
        </div>

           <table id="category_table" cellspacing="0" class="tablesorter">
            <thead>
                <tr>
                    <th width="30" ><input type="checkbox" id="select_all_mail"></th>
                    <th width="30" ></th>
                    <th width="65" >Date</th>
                    <th width="120" >From</th>
                    <th width="120" >Subject</th>
                    <th width="60" >Item #</th>
                    <th width="70" >Actions</th>

                </tr>
            </thead>
            <tbody>
                <?php
                $i = 0;
               if(!empty($messages)){
                foreach ($messages as $message):
                    $class = null;
                    if ($i++ % 2 == 0) :
                        $class = ' class="alt"';
                ?>
                <tr class="<?php echo 'act'.$message['MessageTrack']['id'];?>">
                    <td style="vertical-align: middle;"><input type="checkbox" id="mail" class="mail" value="<?php echo $message['MessageTrack']['id'];?>"></td>
                    <td >
                    <?php if($message['MessageTrack']['star']==1){?>    
                        <a href="javascript:void(0);" class="start mail_star1" id="<?php echo $message['MessageTrack']['id'];?>"></a>
                    <?php }else{?>
                        <a href="javascript:void(0);" class="unstart mail_star1" id="<?php echo $message['MessageTrack']['id'];?>"></a>
                    <?php } ?>
                    </td>
                    <td class="msgcontent" id="<?php echo $message['MessageTrack']['id']; ?>"><?php echo date('m-d-Y',strtotime($message['Message']['created']));?></td>
                    <td class="msgcontent" id="<?php echo $message['MessageTrack']['id']; ?>"><?php if($session->read('Auth.User.id')==$message['MessageTrack']['to_id']){echo $message['Sender']['username'];echo ' (inbox)';}else{if($message['MessageTrack']['to_id']==0){echo $message['MessageTrack']['to_group'];}else{echo $message['Recipient']['username'];}echo ' (sent)';}?></td>
                    <td class="msgcontent" id="<?php echo $message['MessageTrack']['id']; ?>"><?php echo $message['Message']['subject'];?></td>
                    <td class="msgcontent" id="<?php echo $message['MessageTrack']['id']; ?>"><?php echo $message['Message']['item'];?></td>
                    <td><?php
		$options=array('delete'=>'Detele','star'=>'Mark as Star','unstar'=>'Unstar','inbox'=>'move to inbox/sent');
		echo $this->Form->select('action',$options,null,array('empty'=>'Select an action','id'=>$message['MessageTrack']['id'], 'onchange' => 'delete_action(this)'));
		?></td>
                </tr>
                <?php else: ?>
                <tr class="<?php echo 'act'.$message['MessageTrack']['id'];?>">
                    <td style="vertical-align: middle;" class="alt"><input type="checkbox" id="mail" class="mail" value="<?php echo $message['MessageTrack']['id'];?>"></td>
                    <td class="alt">
                         <?php if($message['MessageTrack']['star']==1){?>    
                        <a href="javascript:void(0);" class="start mail_star1" id="<?php echo $message['MessageTrack']['id'];?>"></a>
                    <?php }else{?>
                        <a href="javascript:void(0);" class="unstart mail_star1" id="<?php echo $message['MessageTrack']['id'];?>"></a>
                    <?php } ?>
                    </td>
                     <td class="alt msgcontent" id="<?php echo $message['MessageTrack']['id']; ?>"><?php echo date('m-d-Y',strtotime($message['Message']['created']));?></td>
                     <td class="alt msgcontent" id="<?php echo $message['MessageTrack']['id']; ?>"><?php if($session->read('Auth.User.id')==$message['MessageTrack']['to_id']){echo $message['Sender']['username'];echo ' (inbox)';}else{echo $message['Recipient']['username'];echo ' (sent)';}?></td>
                     <td class="alt msgcontent" id="<?php echo $message['MessageTrack']['id']; ?>"><?php echo $message['Message']['subject'];?></td>
                     <td class="alt msgcontent" id="<?php echo $message['MessageTrack']['id']; ?>"><?php echo $message['Message']['item'];?></td>
                     <td class="alt"><?php
		$options=array('delete'=>'Detele','star'=>'Mark as Star','unstar'=>'Unstar','inbox'=>'move to inbox/sent');
		echo $this->Form->select('action',$options,null,array('empty'=>'Select an action','id'=>$message['MessageTrack']['id'],'onchange' => 'delete_action(this)'));
		?></td>
                </tr>
                <?php endif;
                            endforeach;
                            }else{
                            ?>
                                <tr><td colspan="7">No Message found!</td></tr>
                            <?php
                            }
                            ?>
                    <tbody>
                </table>
        <div>
           <input type="button" class="delete_selected" value="Delete Selected" name="Delete Selected" />&nbsp;
            <input type="button" class="star_selected" value="Star Selected" name="Star Selected" />&nbsp;
            <input type="button" class="unstar_selected" value="Unstar Selected" name="Unstar Selected" />&nbsp;
            <input type="button" class="move_selected" value="Move Selected to Inbox/Sent Items" name="INbox Selected" />&nbsp;
        </div>
        <div id="message_content">
          <?php //Message:: details are here. ?>
        </div>
        <div class="paging">
            <?php
                if($this->params['action']=='backstage_msg_search'){
                $this->Paginator->options(array('url' => "$serchmsg/$type"));
                }
                ?>
		<?php echo $this->Paginator->prev('<< ' . __('previous', true), array(), null, array('class'=>'disabled'));?>
	  	|<?php echo $this->Paginator->numbers();?> |
		<?php echo $this->Paginator->next(__('next', true) . ' >>', array(), null, array('class' => 'disabled'));?>
	</div>
            </div>
            <div class="div_bottom"></div>
        </div>

        <!-- edit sidebar items here -->

        </div>
<?php
                        echo $this->Html->css('table_css');
?>

<?php echo $html->script(array("jquery.pagination.js", "jquery.tablesorter.min.js", "jquery.tablednd.js","jquery.validate.js")); ?>
<script type="text/javascript">

    jQuery(document).ready(function(){
        jQuery("#category_table").tablesorter({headers: {0:{sorter: false}, 1:{sorter: false}, 6:{sorter: false}}});

        var v = jQuery("#searchfrm").validate({

                       });

        jQuery("table#category_table tbody tr").hover(
          function () {
              jQuery(this).children().css('cursor','pointer');
            if(jQuery(this).children().hasClass("alt")){
                jQuery(this).children().removeClass("alt");
                jQuery(this).children().addClass("hovercolor");                

            }   else{
                jQuery(this).children().addClass("hovercolor");
            }
          },
          function () {
            jQuery(this).children().removeClass("hovercolor");

            jQuery("table#category_table tbody tr:odd").children().addClass("alt");
            }
         
        );

		jQuery(".msgcontent").click(function()
			{

                            var msgid=jQuery(this).attr('id');

                            jQuery(this).parent().siblings().removeClass('hovercolor1');
                            jQuery(this).parent().addClass('hovercolor1');
                           // jQuery("#"+msgid).next().addClass("hovercolor1");
                           jQuery.ajax({
                                url : "<?php echo Configure::read('base_url'); ?>messages/view_archive/"+msgid,
                                        success : function (html) {
                                            document.getElementById("message_content").innerHTML = "";
                                            jQuery("#message_content").append(html);
                                            }
                                            });

                               });

                //Check all
		jQuery("#select_all_mail").click(function()
			{
				var checked_status = this.checked;
				jQuery("input[id=mail]").each(function()
				{       var mid= jQuery(this).val();
					this.checked = checked_status;
                                        if(checked_status==true){
                                         jQuery('.act'+mid).addClass('hovercolor1');
                                        }else{
                                            jQuery('.act'+mid).removeClass('hovercolor1');
                                        }
				});
			});
                        //individual selection
		jQuery(".mail").click(function()
			{
                        //alert('hey');
                        var checked_status = this.checked;
                        var mid= jQuery(this).val();
                        //alert(mid);
                         if(checked_status==true){
                         jQuery('.act'+mid).addClass('hovercolor1');
                           }else{
                         jQuery('.act'+mid).removeClass('hovercolor1');
                          }
			});
                        //delete all selected 
		jQuery(".delete_selected").click(function()
			{				
                                var check=false;
				jQuery("input[id=mail]").each(function()
				{
                                     //var mid= jQuery(this).val();
                                       var checked_status=this.checked;
                                       if(checked_status==true){
                                            check=true;
                                       }
				});
                                
                         if(check){
                                  var r=confirm("Do you really want to delete the selected message?");
                                 if (r){
                             jQuery("input[id=mail]").each(function()
				{
                                     var mid= jQuery(this).val();
                                      var checked_status=this.checked;
                                       if(checked_status==true){
                                     /////////////////////////////
                                 
                                jQuery.ajax({
                                    url : "<?php echo Configure::read('base_url'); ?>messages/delete/"+mid,
                                        success : function (html) {
                                            jQuery('.hovercolor1').remove();
                                            //document.getElementById("message_content").innerHTML = "";
                                            //jQuery("#message_content").append(html);
                                            }
                                            });
                                        
                                     /////////////////////////////////
                                       }
				});
                               }
                               jQuery('#message_success').html('Message deleted successfully.');
                                jQuery('#message_success').addClass('msg_success');
                                jQuery("#message_success").show();
                                jQuery("#message_success").animate({ top: "0px"}, 1000 ).fadeOut(1000);
                               }else{
                                   alert('Please select at least One Message!');
                                   return false;
                               }
                               
			});

                        //move inbox all selected
		jQuery(".move_selected").click(function()
			{
                                var check=false;
				jQuery("input[id=mail]").each(function()
				{
                                     //var mid= jQuery(this).val();
                                       var checked_status=this.checked;
                                       if(checked_status==true){
                                            check=true;
                                       }
				});

                         if(check){
                                  var r=confirm("Do you really want to move the selected message to inbox/sent?");
                                 if (r){
                             jQuery("input[id=mail]").each(function()
				{
                                     var mid= jQuery(this).val();
                                      var checked_status=this.checked;
                                       if(checked_status==true){
                                     /////////////////////////////
                                jQuery.ajax({
                                    url : "<?php echo Configure::read('base_url'); ?>messages/move_inbox/"+mid,
                                        success : function (html) {
                                            jQuery('.hovercolor1').remove();
                                            //document.getElementById("message_content").innerHTML = "";
                                            //jQuery("#message_content").append(html);
                                            }
                                            });
                                     /////////////////////////////////
                                       }
				});
                               }
                               jQuery('#message_success').html('Message moved to inbox/sent successfully.');
                                jQuery('#message_success').addClass('msg_success');
                                jQuery("#message_success").show();
                                jQuery("#message_success").animate({ top: "0px"}, 1000 ).fadeOut(1000);
                               }else{
                                   alert('Please select at least One Message!');
                                   return false;
                               }

			});

                         
                         //star all selected
		jQuery(".star_selected").click(function()
			{
                                var check=false;
				jQuery("input[id=mail]").each(function()
				{
                                     //var mid= jQuery(this).val();
                                       var checked_status=this.checked;
                                       if(checked_status==true){
                                            check=true;
                                       }
				});

                         if(check){
                                //  var r=confirm("Do you really want to move the selected message to Archive?");
                                 //if (r){
                             jQuery("input[id=mail]").each(function()
				{
                                     var mid= jQuery(this).val();
                                      var checked_status=this.checked;
                                       if(checked_status==true){
                                     /////////////////////////////
                                jQuery.ajax({
                                    url : "<?php echo Configure::read('base_url'); ?>messages/star/"+mid,
                                        success : function (html) {
                                             jQuery('.act'+mid+' td:eq(1) a').addClass('start');
                                             jQuery('.act'+mid+' td:eq(1) a').removeClass('unstart');
                                            }
                                            });
                                     /////////////////////////////////
                                       }
				});
                              // }
                              jQuery('#message_success').html('Mark as star successfully.');
                                jQuery('#message_success').addClass('msg_success');
                                jQuery("#message_success").show();
                                jQuery("#message_success").animate({ top: "0px"}, 1000 ).fadeOut(1000);
                               }else{
                                   alert('Please select at least One Message!');
                                   return false;
                               }

			});

                         //unstar all selected
		jQuery(".unstar_selected").click(function()
			{
                                var check=false;
				jQuery("input[id=mail]").each(function()
				{
                                     //var mid= jQuery(this).val();
                                       var checked_status=this.checked;
                                       if(checked_status==true){
                                            check=true;
                                       }
				});

                         if(check){
                                //  var r=confirm("Do you really want to move the selected message to Archive?");
                                 //if (r){
                             jQuery("input[id=mail]").each(function()
				{
                                     var mid= jQuery(this).val();
                                      var checked_status=this.checked;
                                       if(checked_status==true){
                                     /////////////////////////////
                                jQuery.ajax({
                                    url : "<?php echo Configure::read('base_url'); ?>messages/unstar/"+mid,
                                        success : function (html) {
                                             jQuery('.act'+mid+' td:eq(1) a').addClass('unstart');
                                             jQuery('.act'+mid+' td:eq(1) a').removeClass('start');
                                            }
                                            });
                                     /////////////////////////////////
                                       }
				});
                              // }
                              jQuery('#message_success').html('Mark as unstar successfully.');
                                jQuery('#message_success').addClass('msg_success');
                                jQuery("#message_success").show();
                                jQuery("#message_success").animate({ top: "0px"}, 1000 ).fadeOut(1000);
                               }else{
                                   alert('Please select at least One Message!');
                                   return false;
                               }

			});
//////////////////////////delete mail////////////////////
                        jQuery(".mail_delete").live('click', function()
			{
                            //alert('ddd');
                            
                            var msgid=jQuery(this).attr('id');
                            var mval=jQuery(this).val();
                           /// alert(mval);
                            //die();
                         var r=confirm("Do you really want to delete the message?");
                         if (r){
                           jQuery.ajax({
                                url : "<?php echo Configure::read('base_url'); ?>messages/delete/"+msgid,
                                        success : function (html) {
                                            jQuery('.hovercolor1').remove();
                                            document.getElementById("message_content").innerHTML = "";
                                            //jQuery("#message_content").append(html);
                                            }
                                            });
                                jQuery('#message_success').html('Message deleted successfully.');
                                jQuery('#message_success').addClass('msg_success');
                                jQuery("#message_success").show();
                                jQuery("#message_success").animate({ top: "0px"}, 1000 ).fadeOut(1000);
                                        }
                               });

                               //////////////////////////move mail////////////////////
                        jQuery(".mail_move").live('click', function()
			{
                           var msgid=jQuery(this).attr('id');
                           var r=confirm("Do you really want to move the message to Inbox/sent Items?");
                         if (r){
                           jQuery.ajax({
                                url : "<?php echo Configure::read('base_url'); ?>messages/move_inbox/"+msgid,
                                        success : function (html) {
                                            jQuery('.hovercolor1').remove();
                                            document.getElementById("message_content").innerHTML = "";
                                            //jQuery("#message_content").append(html);
                                            }
                                            });
                                jQuery('#message_success').html('Moved to inbox/sent successfully.');
                                jQuery('#message_success').addClass('msg_success');
                                jQuery("#message_success").show();
                                jQuery("#message_success").animate({ top: "0px"}, 1000 ).fadeOut(1000);
                                        }
                               });
                               
                                 //////////////////////////star mail////////////////////
                        jQuery(".mail_star").live('click', function()
			{
                           var msgid=jQuery(this).attr('id');
                          
                        // if (r){
                           jQuery.ajax({
                                url : "<?php echo Configure::read('base_url'); ?>messages/star_unstar/"+msgid,
                                        success : function (html) {
                                               if(jQuery('.hovercolor1 td:eq(1) a').hasClass('start')){
                                                  jQuery('.hovercolor1 td:eq(1) a').addClass('unstart');
                                                  jQuery('.hovercolor1 td:eq(1) a').removeClass('start');
                                                  jQuery('#message_success').html(html);
                                               }
                                               else {
                                                  jQuery('.hovercolor1 td:eq(1) a').addClass('start');
                                                  jQuery('.hovercolor1 td:eq(1) a').removeClass('unstart');
                                                  jQuery('#message_success').html(html);
                                               }
                                            }
                                            });
                                            
                                jQuery('#message_success').addClass('msg_success');
                                jQuery("#message_success").show();
                                jQuery("#message_success").animate({ top: "0px"}, 1000 ).fadeOut(1000);
                                       // }
                               });

                                  //////////////////////////star unstar mail////////////////////
                        jQuery(".mail_star1").click(function()
			{
                           var msgid=jQuery(this).attr('id');
                          
                        // if (r){
                           jQuery.ajax({
                                url : "<?php echo Configure::read('base_url'); ?>messages/star_unstar/"+msgid,
                                        success : function (html) {
                                               if(jQuery('.act'+msgid+' td:eq(1) a').hasClass('start')){
                                                  jQuery('.act'+msgid+' td:eq(1) a').addClass('unstart');
                                                  jQuery('.act'+msgid+' td:eq(1) a').removeClass('start');
                                                 jQuery('#message_success').html(html);
                                               }
                                               else {
                                                  jQuery('.act'+msgid+' td:eq(1) a').addClass('start');
                                                  jQuery('.act'+msgid+' td:eq(1) a').removeClass('unstart');
                                                  jQuery('#message_success').html(html);
                                               }
                                            }
                                            });
                                jQuery('#message_success').addClass('msg_success');
                                jQuery("#message_success").show();
                                jQuery("#message_success").animate({ top: "0px"}, 1000 ).fadeOut(1000);
                                       // }
                               });
    });

        function delete_action(param){
        var mval=jQuery(param).val();
        var mid=jQuery(param).attr('id');
        
        ////////////////////////delete//////////
        if(mval=='delete'){
            
            var r=confirm("Do you really want to delete the message?");
            if (r){
            jQuery.ajax({
            url : "<?php echo Configure::read('base_url'); ?>messages/delete/"+mid,
            success : function (html) {
            jQuery('.act'+mid).remove();
            }
            });
            jQuery('#message_success').html('Message deleted successfully.');
                                jQuery('#message_success').addClass('msg_success');
                                jQuery("#message_success").show();
                                jQuery("#message_success").animate({ top: "0px"}, 1000 ).fadeOut(1000);
                }
            }
            
             ////////////////////////archieve//////////
             if(mval=='inbox'){
             
           var r=confirm("Do you really want to move the message to inbox/sent?");
            if (r){
            jQuery.ajax({
            url : "<?php echo Configure::read('base_url'); ?>messages/move_inbox/"+mid,
            success : function (html) {
          jQuery('.act'+mid).remove();
            }
            });
            jQuery('#message_success').html('Moved to inbox/sent successfully.');
                                jQuery('#message_success').addClass('msg_success');
                                jQuery("#message_success").show();
                                jQuery("#message_success").animate({ top: "0px"}, 1000 ).fadeOut(1000);
            }
            }

             ////////////////////////star//////////
             if(mval=='star'){
                      
           //var r=confirm("Do you really want to move the message to Archieve?");
           // if (r){
            jQuery.ajax({
            url : "<?php echo Configure::read('base_url'); ?>messages/star/"+mid,
            success : function (html) {
            jQuery('.act'+mid+' td:eq(1) a').addClass('start');
            jQuery('.act'+mid+' td:eq(1) a').removeClass('unstart');
            }
            });
            jQuery('#message_success').html('Mark as star successfully.');
                                jQuery('#message_success').addClass('msg_success');
                                jQuery("#message_success").show();
                                jQuery("#message_success").animate({ top: "0px"}, 1000 ).fadeOut(1000);
           // }
            }

             ////////////////////////unstar//////////
             if(mval=='unstar'){
            //var r=confirm("Do you really want to move the message to Archieve?");
           // if (r){
            jQuery.ajax({
            url : "<?php echo Configure::read('base_url'); ?>messages/unstar/"+mid,
            success : function (html) {
            jQuery('.act'+mid+' td:eq(1) a').addClass('unstart');
            jQuery('.act'+mid+' td:eq(1) a').removeClass('start');
            }
            });
            jQuery('#message_success').html('Mark as unstar successfully.');
                                jQuery('#message_success').addClass('msg_success');
                                jQuery("#message_success").show();
                                jQuery("#message_success").animate({ top: "0px"}, 1000 ).fadeOut(1000);
           // }
            }
            
        }
</script>